// pages/user/couponOld/couponOld.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    use: '../../../images/use.png',
    old: '../../../images/old.png',
    coupon2: '../../../images/coupon2.png',
    list:[],
    putTypes: [
      {
        id: 1,
        name: '新用户注册'
      },
      {
        id: 2,
        name: '线下发放'
      },
      {
        id: 3,
        name: '免费领取'
      },
      {
        id: 4,
        name: '抽奖领取'
      },

    ],
    empty: true,
    hasMore: false, //数据是否加载完成
    loading: false,
    load_show: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getCouponList();  // 获取优惠券列表
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    _PAGE++;
    that.getCouponList();
  
  },
  // 获取列表
  getCouponList() {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'User/Coupon/meList',
      data: {
        type: 2,
        page: _PAGE,
        pageSize: _PAGESIZE,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              list: res.data.data,
              empty: false
            });
          } else {
            that.setData({
              list: that.data.list.concat(res.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })
        console.log('优惠券列表', res)
      }
    })
  },
  
})